package lk.icta.core.dao.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.ObjectionDAO;
import lk.icta.core.entity.Objection;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.List;

/**
 * @author Kusal Samarakoon
 */
public class ObjectionDAOImpl extends BaseDAO implements ObjectionDAO {

    @Transactional(propagation = Propagation.MANDATORY)
    public void addObjection(Objection objection) {
        logger.debug("Adding a objection {}", objection.getId());
        em.persist(objection);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<Objection> getObjectionByApplicationId(long id) {
        logger.debug("Get a objection {}", id);
        Query q = em.createNamedQuery("getObjectionByApplicationId");
        q.setParameter("applicationId", id);
        //return (Objection) q.getSingleResult();
        if (q.getResultList().size() > 0) {
            return  q.getResultList();
        } else {
            return null;
        }
    }


}
